package com.chzc.fm.duotin.ui.mine;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.LikeRecyclerAdapter;
import com.chzc.fm.duotin.bean.mine.LikeBean;
import com.chzc.fm.duotin.ui.mine.utils.MineUrl;
import com.chzc.fm.duotin.ui.other.BaseActivity;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.Unicode2CN;
import com.chzc.fm.duotin.widget.TitleBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class LikeActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private TitleBar titleBar;

    private int page = 1;
    private int totalPage;
    private List<LikeBean> likeBeanList;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_like;
    }

    @Override
    protected void initViews() {
        recyclerView = (RecyclerView) findViewById(R.id.rv_like_content);
        titleBar = (TitleBar) findViewById(R.id.title_bar);
    }

    @Override
    protected void initEvents() {

    }

    @Override
    protected void init() {
        titleBar.setLeft(true, R.drawable.arrow_back_white);
        titleBar.setTitleBarVisibility(true, "赞");

        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                return Unicode2CN.unicode2GBK((String) HttpUtil.doGet(MineUrl.getLikeUrl(page)));
            }
        };
        DuotinTask.IRequestCallback callback = new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {
                try {
                    JSONObject root = new JSONObject(object.toString());
                    String msg = root.getString(GlobalParam.ERROR_MSG);
                    if(GlobalParam.SUCCESS.equals(msg.toLowerCase())){
                        JSONObject data = root.getJSONObject("data");
                        JSONObject pageBean = data.getJSONObject("page");
                        JSONArray dataList = data.getJSONArray("data_list");
                        likeBeanList = LikeBean.arrayLikeBeanFromData(dataList.toString());

                        totalPage = pageBean.getInt("total_page");
                        if(totalPage > page){
                            page++;
                        }
                        setData();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void error(String msg) {

            }
        };

        DuotinTask task = new DuotinTask(request, callback);
        putAsyncTask(task);
    }

    private void setData() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        LikeRecyclerAdapter adapter = new LikeRecyclerAdapter(getBaseContext(), likeBeanList);
        recyclerView.setAdapter(adapter);
    }
}
